<template>
  <div class="line-week-wrap">
    <base-charts id="lineWeekDomId" style="margin-top: 10px" height="18vh" :option="weekLineOption"
      @inited="weekChartInited"></base-charts>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import BaseCharts from '@/components/charts/base-charts.vue'
import { get7DayHandlerTrend } from "@/api/h3yun"

export default {
  name: 'line-Week',
  components: {
    BaseCharts
  },
  props: ['areaCode'],
  data() {
    return {
      list: [],
      timer: null,
      activeIndex: 1,
    }
  },
  watch: {
    areaCode: {
      handler(val) {
        this.initData()
      },
      true: true,
      immediate: true
    }
  },
  methods: {
    async initData() {
      let res = await get7DayHandlerTrend(this.areaCode)
      const resList = res.data.map((item) => ({
        name: item.name,
        value: item.handlerTotal
      }))
      this.list = resList
    },
    weekChartInited() {
      this.weekChart = echarts.init(document.getElementById('lineWeekDomId'))
    }
  },
  mounted() {
    // 设置定时器，每分钟刷新一次数据
    this.timer = setInterval(() => {
      this.initData()
    }, 60000)
  },
  beforeDestroy() {
    // 组件销毁前清除定时器
    if (this.timer) {
      clearInterval(this.timer)
      this.timer = null
    }
  },
  computed: {
    weekLineOption() {
      let list = this.list
      return {
        title: {
          text: '次',
          top: '1%',
          left: '3.5%',
          textStyle: {
            fontSize: 12,
            color: '#fff',
          }
        },
        tooltip: {
          trigger: 'axis',
          formatter: '时间：{b0}<br/> 数量：{c0} 条',
          backgroundColor: 'rgba(25,72,124,1)',
          borderColor: 'rgba(110,212,255,1)',
          textStyle: {
            fontSize: 12,
            color: '#fff',
          }
        },
        grid: {
          left: '5%',
          right: '2%',
          top: '20%',
          bottom: '2%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: list.map(it => it.name),
          axisLabel: {
            color: '#fff'
          }
        },
        yAxis: {
          type: 'value',
          splitNumber: 3,
          axisLabel: {
            color: '#fff',
          },
          axisTick: {
            show: false
          },
          splitLine: {
            lineStyle: {
              type: 'dashed',
              color: 'rgba(54,83,125,0.75)'
            }
          }
        },
        series: [
          {
            data: list.map(it => it.value),
            type: 'line',
            symbol: 'emptyCircle',
            symbolSize: 6,
            smooth: true, //平滑曲线显示
            areaStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(
                  0,
                  0,
                  0,
                  1,
                  [
                    {
                      offset: 1,
                      color: 'rgba(52, 238, 226, 0)',
                    },
                    {
                      offset: 0,
                      color: '#14FFF1',
                    },
                  ],
                  false
                ),
                shadowColor: 'rgba(0, 0, 0, 0.1)',
                shadowBlur: 10,
              },
            },
            lineStyle: {
              color: 'rgba(20, 255, 241, 1)'
            },
            label: {
              show: false
            }
          }
        ]
      }
    }
  },
}
</script>

<style lang="scss">
.el-popover {
  background: rgba(20, 65, 126, 0.95);
  padding: 0;
}

.el-popover[x-placement^=bottom] .popper__arrow {
  border-right-color: rgba(20, 65, 126, 0.95);
  border-bottom-color: rgba(20, 65, 126, 0.95);
  box-shadow: none;
}
</style>
<style scoped lang="scss">
.my-select {
  padding: 8px 0;

  .my-option {
    height: 28px;
    line-height: 28px;
    color: #fff;
    cursor: pointer;
    padding: 0 8px;

    &:hover {
      background: #1d73de85;
    }

    &.active {
      background: #1d73de;
      color: #fff;
    }
  }
}

.line-week-wrap {
  margin-top: 10px;
  position: relative;
}
</style>
